package coe.pumbaa

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured

@Secured(['IS_AUTHENTICATED_REMEMBERED'])
class RoleController {

    static allowedMethods = [save: "POST", update: "POST"]
    def springSecurityService
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        if(springSecurityService.currentUser.role.level <= 0){
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [roleInstanceList: Role.list(params), roleInstanceTotal: Role.count(),
                roleInstance: new Role(),navigation:[pos:"manage_role"]]
        }else{
            flash.warning = "You are not admin."
            redirect(controller:'home',action:'index')
        }
    }

    def save() {
        if(springSecurityService.currentUser.role.level <= 0){
            def roleInstance = new Role(params)
            if (!roleInstance.save(flush: true)) {
                flash.error = "Error! Check your input."
            }else{
                flash.success = message(code: 'default.created.message', args: [message(code: 'role.label', default: 'Role'), roleInstance.id])
            }
            redirect(action: "list")
        }else{
            flash.warning = "You are not admin."
            redirect(controller:'home',action:'index')
        }
    }

    def edit() {
        if(springSecurityService.currentUser.role.level <= 0){
            def roleInstance = Role.get(params.id)
            if (!roleInstance) {
                flash.error = message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])
                redirect(action: "list")
                return
            }
            [roleInstance: roleInstance,navigation:[pos:"manage_role"]]
        }else{
            flash.warning = "You are not admin."
            redirect(controller:'home',action:'index')
        }
    }

    def update() {
        if(springSecurityService.currentUser.role.level <= 0){
            def roleInstance = Role.get(params.id)
            roleInstance.properties = params
            if(!roleInstance.save(flush: true)){
                flash.error = "Error! Check your input."
                redirect(action: 'edit', id:params.id)
            }else{
                flash.success = message(code: 'default.updated.message', args: [message(code: 'role.label', default: 'Role'), roleInstance.id])
            }
            redirect(action: "list")
        }else{
            flash.warning = "You are not admin."
            redirect(controller:'home',action:'index')
        }
    }

    def delete() {
        if(springSecurityService.currentUser.role.level <= 0){
            def roleInstance = Role.get(params.id)
            if(!roleInstance.delete(flush: true)){
                flash.error = "Error! Can not delete."
            }else{
                flash.success = message(code: 'default.deleted.message', args: [message(code: 'role.label', default: 'Role'), roleInstance.id])
            }
            redirect(action: "list")
        }else{
            flash.warning = "You are not admin."
            redirect(controller:'home',action:'index')
        }
    }
}
